<template>
  <a-modal
    class="fillmaskModal"
    title="编辑章节"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top: 5%; height: 85%; overflow-y: hidden"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model
        ref="form"
        v-bind="layout"
        :model="model"
        :rules="validatorRules"
      >
        <a-form-model-item label="章节名称">
          <a-input v-model="model.template" placeholder="请输入模板名称" />
        </a-form-model-item>
        <div class="a_btn">
          <a-button type="primary" style="text-align: center" @click="save()"> 提交 </a-button>
        </div>
        <div class="shortcut">章节快捷命名</div>
        <div class="designation_box">
          <div class="designation" v-for="(item,index) in list" :key="index" @click="addDesignation(item)">{{item}}</div>

        </div>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { addRole, editRole, duplicateCheck } from "@/api/api";
import bus from "@/bus";
export default {
  name: "RoleModal",
  data() {
    return {
        list:[
            '报告主体信息','温室气体排放','活动数据及来源说明','排放因子数据及来源说明'
        ],
      title: "操作",
      visible: false,
      roleDisabled: false,
      model: {
        template: "",
      },
      layout: {
        labelCol: { span: 3 },
        wrapperCol: { span: 14 },
      },
      confirmLoading: false,
      validatorRules: {
        consume: [{ required: true, message: "请填写消耗量!" }],
        roleCode: [
          { required: true, message: "请输入角色名称!" },
          { min: 0, max: 64, message: "长度不超过 64 个字符", trigger: "blur" },
          { validator: this.validateRoleCode },
        ],
        description: [
          {
            min: 0,
            max: 126,
            message: "长度不超过 126 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
      save(val){
        bus.$emit('designation')
          this.visible = false
      },
      addDesignation(val){
          this.model.template = val
      },
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
      //编辑页面禁止修改角色编码
      if (this.model.id) {
        this.roleDisabled = true;
      } else {
        this.roleDisabled = false;
      }
    },
    close() {
      //   this.$refs.form.clearValidate();
      this.$emit("close");
      this.visible = false;
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true;
          let obj;
          if (!this.model.id) {
            obj = addRole(this.model);
          } else {
            obj = editRole(this.model);
          }
          obj
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit("ok");
              } else {
                that.$message.warning(res.message);
              }
            })
            .finally(() => {
              that.confirmLoading = false;
              that.close();
            });
        } else {
          return false;
        }
      });
    },
    handleCancel() {
      this.close();
    },
    validateRoleCode(rule, value, callback) {
      if (/[\u4E00-\u9FA5]/g.test(value)) {
        callback("角色编码不可输入汉字!");
      } else {
        let params = {
          tableName: "sys_role",
          fieldName: "role_code",
          fieldVal: value,
          dataId: this.model.id,
        };
        duplicateCheck(params).then((res) => {
          if (res.success) {
            callback();
          } else {
            callback(res.message);
          }
        });
      }
    },
  },
};
</script>

<style lang="scss" >
@import "@/assets/css/newrModule.scss";
.fillmaskModal .ant-modal-body {
  padding-top: 0px !important;
  padding-left: 0;
  padding-right: 0;
}
.fillmaskModal .ant-spin-nested-loading {
  padding: 12px 24px !important;
}
.fillmaskModal .ant-select-selection-selected-value {
  font-size: 12px;
}
.fillmaskModal .info {
  background: #ebf5fe;

  background-image: linear-gradient(
    rgb(209, 231, 253),
    rgba(245, 249, 253, 0.644)
  );
  padding: 12px 24px !important;
  margin-bottom: 10px;
  //   border-bottom: 1px dashed rgb(241, 241, 241);
  p {
    font-size: 14px;
    font-weight: 600;
    margin: 0;
    margin-bottom: 4px;
  }
}
.shortcut {
  width: 100%;
  background: #eeeeee;
  height: 32px;
  line-height: 32px;
  text-align: center;
}
.a_btn {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  margin-bottom: 30px;
}
.designation_box {
  padding-top: 10px;
  display: flex;
  align-items: center;
  flex-direction: column;
}
.designation {
  background: #006eff;
  color: #fff;
  font-size: 12px;
  margin-top: 6px;
  border-radius: 6px;
  text-align: center;
  width: 200px;
  padding: 4px 0;
  cursor: pointer;
  &:hover{
      opacity: .9;
      transition: .2s;
  }
}
</style>